package com.zwb.demo.easyweb.modules.database.controller;

import com.zwb.demo.easyweb.common.entity.response.ResponseResult;
import com.zwb.demo.easyweb.modules.database.service.DatabaseTableService;
import com.zwb.demo.easyweb.modules.database.vo.DatabaseTableVO;
import com.zwb.demo.easyweb.modules.database.vo.ImportTableReqVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/database")
@Api(value = "数据库表接口" ,description = "数据库表接口")
public class DatabaseTableController {


    private final DatabaseTableService databaseTableService;

    @Autowired
    public DatabaseTableController(DatabaseTableService databaseTableService) {
        this.databaseTableService = databaseTableService;
    }


    @GetMapping("/{uuid}/tables")
    @ApiOperation(value = "根据数据库id获取所有表")
    public ResponseResult findAllByDatabaseLinkId(@PathVariable String uuid){
        return databaseTableService.findAllByDatabaseLinkId(uuid);
    }

    @GetMapping("/{uuid}/jdbcTables")
    @ApiOperation(value = "根据数据库id获取所有jdbc表")
    public ResponseResult findJdbcTableByDatabaseLinkId(@PathVariable String uuid){
        return databaseTableService.findJdbcTableByDatabaseLinkId(uuid);
    }

    @PostMapping("/table/import")
    @ApiOperation(value = "从数据库导入表")
    public ResponseResult importFromDatabase(@RequestBody ImportTableReqVO importTableReqVO){
        return databaseTableService.importFromDatabase(importTableReqVO.getDatabaseId(), importTableReqVO.getTables());
    }

    @DeleteMapping("/table/{uuid}")
    @ApiOperation(value = "删除表")
    public ResponseResult deleteOne(@PathVariable String uuid){
        return databaseTableService.deleteOne(uuid);
    }

    @PutMapping("/table/")
    @ApiOperation(value = "新增或保存表")
    public ResponseResult saveOne(@RequestBody DatabaseTableVO vo){
        return databaseTableService.saveOne(vo);
    }
}